package com.greate.community.controller;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.constant.FileType;
import com.greate.community.entity.Result;
import com.greate.community.service.CosService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 获取腾讯云COS上传凭证
 */
@RestController
@RequestMapping("/cos")
public class CosController extends BaseController {

    @Autowired
    private CosService cosService;

    @GetMapping("/credential")
    public Result getCredential(@RequestParam("type") String type, @RequestParam("fileName") List<String> fileNames) {
        if (StringUtils.isBlank(type) || fileNames.isEmpty() || !FileType.isValid(fileNames)) {
            return new Result("fail", "1", "the request parameters are invalid");
        }
        JSONObject response = cosService.getCredential(getUser().getId(), type, fileNames);
        if (response == null) {
            return new Result("fail", "1", "the request parameters are invalid");
        }
        return new Result("success", Result.SUCCESS, response);
    }

    @GetMapping("/admin/credential")
    public Result getAdminCredential(@RequestParam("type") String type, @RequestParam("filePath") List<String> filePaths) {
        if (StringUtils.isBlank(type) || filePaths.isEmpty() || !FileType.isValid(filePaths)) {
            return new Result("fail", "1", "the request parameters are invalid");
        }
        JSONObject response = cosService.getAdminCredential(type, filePaths);
        if (response == null) {
            return new Result("fail", "1", "the request parameters are invalid");
        }
        return new Result("success", Result.SUCCESS, response);
    }
}
